<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>修改员工</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{../../lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{../../css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" th:object="${emp}">
    <div class="layui-form-item">
        <label class="layui-form-label required">员工姓名</label>
        <div class="layui-input-block">
            <input type="text" th:field="*{id}" style="display: none"/>
            <input type="text" th:field="*{employee_id}" style="display: none"/>
            <input type="text" th:field="*{name}" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入员工姓名" value="" class="layui-input">
            <!--            <tip>填写自己管理账号的名称。</tip>-->
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">员工账号</label>
        <div class="layui-input-block">
            <input type="text" th:field="*{username}" lay-verify="required" lay-reqtext="账号不能为空" placeholder="请输入员工账号" value="" class="layui-input">
            <tip>填写员工登陆账号的名称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">员工密码</label>
        <div class="layui-input-block">
            <input type="text" th:field="*{password}" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入员工密码" value="" class="layui-input">
            <!--            <tip>填写员工登陆账号的名称。</tip>-->
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" th:field="*{sex}" value="1" title="男">
            <input type="radio" th:field="*{sex}" value="0" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">年龄</label>
        <div class="layui-input-block">
            <input type="text" th:field="*{age}" placeholder="请输入年龄" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">手机</label>
        <div class="layui-input-block">
            <input type="number" th:field="*{phone}" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">邮箱</label>
        <div class="layui-input-block">
            <input type="email" th:field="*{email}" placeholder="请输入邮箱" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">部门</label>
        <div class="layui-input-block">
            <select name="dept_id" lay-verify="required" lay-filter="levelIdFilter">//name="dept_id"
                <option value="">请选择</option>
                <option th:each="dept:${deptList}" th:value="${dept.id}" th:selected="${emp.department.id eq dept.id}">
                    <span th:text="${dept.name}"></span>
                </option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">级别</label>
        <div class="layui-input-block">
            <select name="level_id" id="level_id" lay-verify="required">
                <option value="">请选择</option>
                <option th:each="level:${levelList}" th:value="${level.id}" th:selected="${emp.level.id eq level.id}">
                    <span th:text="${level.name}"></span>
                </option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label required">员工状态</label>
        <div class="layui-input-block">
            <input type="radio" th:field="*{status}" value="1" title="启用">
            <input type="radio" th:field="*{status}" value="0" title="禁用">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">修改员工</button>
        </div>
    </div>
</div>
<script th:src="@{../../lib/layui/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;



        form.on('select(levelIdFilter)', function (data) {
            $("#level_id").empty();
            var url = "/level/queryLevelByDept";
            $.post(url, {department_name: data.value},
                function (data) {
                    $.each(data, function (i, n) {
                        console.log(n.name);
                        $('#level_id').append(new Option(n.name, n.id));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                });
        });



        form.on('submit(saveBtn)', function (data) {
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $("#btn-submit").attr("disabled", "disabled").addClass("layui-btn-disabled");
            //提交数据
            var url = "/emp/update";
            $.post(url, data.field, function (res) {
                if (res.code === 0) {
                    //成功提示
                    layer.msg(res.message, {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        //关闭窗口,刷新数据表格
                        parent.refreshTable();
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                } else {
                    $("#btn-submit").removeAttr("disabled", "disabled").removeClass("layui-btn-disabled");
                    //失败则提示错误消息,窗口不关闭
                    layer.msg('修改失败,请联系管理员', {
                        icon: 2,
                        time: 2000, //2秒关闭（如果不配置，默认是3秒）
                        anim: 6
                    }, function(){
                        //do something
                    });
                }
            })
            //关掉窗口并刷新
            //失败则提示错误信息
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
</body>
</html>